package com.huluobo.a2202mvvm_im_douyindemo.contact;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseRepo;
import com.hyphenate.EMValueCallBack;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by LC on 2024/4/25.
 */
public class ContactRepo extends BaseRepo {
    public void getAllContacts(MutableLiveData<List<ContactListItemBean>> success, MutableLiveData<String> failed) {
        emClient.contactManager().aysncGetAllContactsFromServer(new EMValueCallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> value) {
                Collections.sort(value);
                //把SDK中返回的好友列表转换为自定义Bean类的好友列表数据源
                //创建自定义Bean的集合
                List<ContactListItemBean> contactListItemBeans = new ArrayList<>();

                //循环取出每一个value的值(username)
                for (int i = 0; i < value.size(); i++) {
                    boolean showFirstLetter = false;
                    //当i等于0时,一定是true,对比与上一个username,如果首字符不同,也是true
                    if (i == 0 || value.get(i).charAt(0) != value.get(i - 1).charAt(0)) {
                        showFirstLetter = true;
                    }
                    //把每一个username封装为自定义Bean
                    ContactListItemBean contactListItemBean = new ContactListItemBean(
                            value.get(i),//(dd1,dd2)
                            Character.toUpperCase(value.get(i).charAt(0)),//d
                            showFirstLetter
                    );
                    //自定义bean的集合添加一个数据源
                    contactListItemBeans.add(contactListItemBean);
                }
                //循环完毕后,给livedata添加数据
                success.postValue(contactListItemBeans);
            }

            @Override
            public void onError(int error, String errorMsg) {
                failed.postValue(errorMsg);
            }
        });
    }
}
